<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_menu_dbc.class.php');


class Vpage_Template_Manage_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('manage', Tangra_Parameter_Method::GET, 'manage'), 'manage_event');
	}


	protected function manage_event($template_id) {
		$dbc = $this->get_dbc();

		$template = new TCMS_VPage_Template_DBC();
		if ($template->load_by_id($dbc, $template_id)) {
			$this->export('template', $template);

			$cis = TCMS_VPT_CI_DBC::load_vpt_cis_for_template($dbc, $template_id);
			$this->export('cis', $cis);

			$sas = TCMS_VPT_System_Attribute_DBC::select_vpt_sas_for_template($dbc, $template_id);
			$this->export('sas', $sas);

			$pages_count = TCMS_VPage_DBC::count_pages_with_template($dbc, $template_id);
			$pages_online_count = TCMS_VPage_DBC::count_pages_with_template($dbc, $template_id, true);
			$this->export('pages_count', $pages_count);
			$this->export('pages_online_count', $pages_online_count);

			$menus = TCMS_VPT_Menu_DBC::select_all_for_template($dbc, $template_id);
			$this->export('menus', $menus);


			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}



		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting manage parameter.');
	}
}